<?php
include_once 'html/register.html';
// header("Content-type: text/html; charset=utf-8");
$username = $_POST['username'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
$sex = $_POST['sex'];
$choose = $_POST['choose'];
$comment = $_POST['comment'];
//提交之前php进行检查
if ($username == '') {
    $nameErr = "请填写用户名";
    exit(0);
}
if ($password == '') {
    $passwordErr = "请填写密码";
    exit(0);
}
if ($password != $repassword) {
    $repasswordErr = "请再次填写密码";
    exit(0);
}
if (strlen($password) < 6 || strlen($repassword) < 6) {
    echo '<script>alert("密码长度不少于6个");history.go(-1);</script>';
    exit(0);
}
//当一切符合要求之后
if ($password == $repassword) {
    $conn = new mysqli('localhost', 'root', '2580', 'hospital'); //连接数据库，输入数据库密码和数据库名字
    if ($conn->connect_error) {
        echo '数据库连接失败！';
        exit(0);
    } else {
        $sql = "select username from userinfo where username = '$_POST[username]'"; //是否用户已经存在
        $result = $conn->query($sql);
        $number = mysqli_num_rows($result);
        if ($number) {
            echo '<script>alert("用户名已经存在");history.go(-1);</script>';
        } else {
            $sql_insert = "insert into userinfo (username,password,sex,choose,comment) values('$_POST[username]','$_POST[password]','$_POST[sex]','$_POST[choose]',$_POST[comment])"; //向数据库中插入信息
            $res_insert = $conn->query($sql_insert);
            if ($res_insert) {
                echo "<script>alert('注册成功');</script>";
            } else {
                echo "<script>alert('系统繁忙，请稍候！');</script>";
            }
        }
    }
} else {
    echo "<script>alert('提交未成功！'); history.go(-1);</script>";
}
include_once 'html/register.html';
